/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#define HUDMODULE
#include "HUDButton.h"

namespace ModularEngine
{
	HUDButton::HUDButton()
	{
		mpButtonText = 0;
		mtFunc = 0;
		LOADCOLORS;
	}

	HUDButton::~HUDButton()
	{
		// Cleanup the button
		delete [] mpButtonText;
	}

	void HUDButton::draw()
	{
		Vector2d cPoint = ModularEngine::WindowModule::getSingleton().getMouse();
		Vector3d baseColor;

		// Set coordinate system around the button.
		gl->setCoords( mvPosition );

		// Does parent have focus?
		bool pFocus = true;

		if( pParent )
		{
			pFocus = pParent == HUDModule::getSingleton().mpHoveredElement;
		}

		// If the last button state is still down, check to see if it's really been released or not.
		if( mbMouseDown )
		{

			if( gl->pointInRect( cPoint, mvPosition, mvDimensions ) && pFocus )
			{
				ModularEngine::WindowModule::getSingleton().setMouse( IDC_HAND );
				SetCursor( LoadCursor( NULL, IDC_HAND ) );
			}
			// If mouse has been released then run the functor
			// unless mouse has come out of the box.
			if( !isMouseDown() )
			{
				mbMouseDown = false;
				if( gl->pointInRect( cPoint, mvPosition, mvDimensions ) )
				{
					// Run the functor
					if( mtFunc )
						(*mtFunc)();
				}
			}

			// Set the base color 2 shades darker for change of state
			baseColor = mvBaseColor*.6;
		} 
		else {

			// If mouse is hovering over the button make a shade darker.
			// If not hover, go back to original color
			if( gl->pointInRect( cPoint, mvPosition, mvDimensions ) && pFocus )
			{
				ModularEngine::WindowModule::getSingleton().setMouse( IDC_HAND );
				SetCursor( LoadCursor( NULL, IDC_HAND ) );
				baseColor = mvBaseColor*.8;
			}
			else
				baseColor = mvBaseColor; 
		}

		glDisable( GL_TEXTURE_2D );

		glColor3f( mvTextColor.x, mvTextColor.y, mvTextColor.z );
		gl->drawRect( Vector2d( 0, 0 ), mvDimensions );

		// Draw the button.  It's a gradient from original color to 1/2 darker
		gl->drawGradientRect( Vector2d( 1, 1 ), mvDimensions-Vector2d(2,2), baseColor, baseColor*.5 );

		// Set color of the text.
		glColor3f( mvTextColor.x, mvTextColor.y, mvTextColor.z );

		// Draw the text over the button.
		if( mpButtonText )
			gl->drawText( mpButtonText, Vector2d( (mvDimensions.x/2)-(strlen(mpButtonText)*9)/2, ((mvDimensions.y-10)/2)-6 ) );

	}

	int HUDButton::initialize()
	{
		// Get OpenglModule
		gl = &ModularEngine::OpenglModule::getSingleton();
		mbMouseDown = false;
		return 1;
	}

	void HUDButton::mouseInput( Vector2d vPos, unsigned int nDown )
	{
		// sets mouse down to true
		mbMouseDown = true;		
	}

	void HUDButton::setCallFunc( Functor *c )
	{
		// Get Call Functor for this button.
		mtFunc = c;
	}

	void HUDButton::keyboardInput(UINT nKey)
	{
		// Well, button's don't do much with keys...
	}
}
